﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Net.NetworkInformation;
using System.Diagnostics;

namespace SHack.Helper
{
    class PortTestHelper
    {
        public static int TimeOut = 500;
        public static bool Ping(string ip)
        {
            Ping ping = new Ping();
            PingReply pingReply = ping.Send(ip);
            return  pingReply.Status == IPStatus.Success;
        }

        public static bool Test(string ip, int port,ref long ticks)
        {
            Stopwatch sw = new Stopwatch();
            var client = new TcpClient();
            sw.Start();
            var result = client.BeginConnect(ip, port, null, null);
            var success = result.AsyncWaitHandle.WaitOne(TimeOut);
            sw.Stop();
            ticks = sw.ElapsedMilliseconds;
            client.Close();
            client = null;
            return success;
        }
    }
}
